/**
 *
 */
package com.wateray.ipassbook.ui.view.contentPanel;

import java.awt.Component;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import com.wateray.ipassbook.domain.Entity;
import com.wateray.ipassbook.kernel.event.IPassbookEvent;
import com.wateray.ipassbook.kernel.event.IPassbookEventType;
import com.wateray.ipassbook.kernel.service.PassbookService;
import com.wateray.ipassbook.ui.action.ActionManager;
import com.wateray.ipassbook.ui.action.AddTransferAction;
import com.wateray.ipassbook.ui.action.ShowPassbookWizardAction;
import com.wateray.ipassbook.ui.model.PassbookTableModel;
import com.wateray.ipassbook.util.LanguageLoader;

/**
 * @author c-bryu
 * 
 */
public class PassbookPage extends AbstractPage {

	/**
	 *serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private static PassbookPage thisPage;

	private JTable passbookTable;
	private PassbookTableModel tableMode;

	//
	private JButton addButton;
	private JButton modifyButton;
	private JButton deleteButton;

	private JButton transferButton;

	/**
	 * @return an instance of the PassbookPage.
	 * */
	public static PassbookPage getInstance() {
		if (thisPage == null) {
			thisPage = new PassbookPage();
		}
		return thisPage;
	}

	private PassbookPage() {
		super();

	}

	@Override
	protected Component getTopPane() {
		super.getTopPane();
		buttonBar.add(getTransferButton());

		buttonBar.add(new JSeparator());
		buttonBar.add(getAddButton());
		// buttonBar.add(getModifyButton());
		// buttonBar.add(getDeleteButton());
		return buttonBar;
	}

	@Override
	protected Component getCenterPane() {
		return new JScrollPane(getPassbookTable());
	}

	/**
	 * @return the addButton
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton(ActionManager
					.getAction(ShowPassbookWizardAction.class));
			addButton.setIcon(null);
		}
		return addButton;
	}

	/**
	 * @return the modifyButton
	 */
	private JButton getModifyButton() {
		if (modifyButton == null) {
			modifyButton = new JButton();

		}
		return modifyButton;
	}

	/**
	 * @return the deleteButton
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();

		}
		return deleteButton;
	}

	/**
	 * @return the transferButton
	 */
	private JButton getTransferButton() {
		if (transferButton == null) {
			transferButton = new JButton();
			AddTransferAction action = (AddTransferAction) ActionManager
					.getAction(AddTransferAction.class);
			// Parameter tableMode
			action.setData(tableMode);
			transferButton.setAction(action);
			transferButton.setIcon(null);
		}
		return transferButton;
	}

	private JTable getPassbookTable() {
		if (passbookTable == null) {
			passbookTable = new JTable();
			passbookTable.setFillsViewportHeight(true);
			passbookTable.getSelectionModel().setSelectionMode(
					ListSelectionModel.SINGLE_SELECTION);
			tableMode = new PassbookTableModel(getColumnNames());
			tableMode.setData(getData());
			passbookTable.setModel(tableMode);
		}
		return passbookTable;
	}

	/***/
	private Vector<String> getColumnNames() {
		Vector<String> columnNames = new Vector<String>();
		columnNames.add(LanguageLoader.getString("No"));
		columnNames.add(LanguageLoader.getString("ID"));
		columnNames.add(LanguageLoader.getString("SubID"));
		columnNames.add(LanguageLoader.getString("Name"));
		columnNames.add(LanguageLoader.getString("Capital"));
		columnNames.add(LanguageLoader.getString("Balance"));
		columnNames.add(LanguageLoader.getString("Currency"));
		columnNames.add(LanguageLoader.getString("Memo"));

		return columnNames;
	}

	public Vector<Entity> getData() {
		Vector<Entity> vect = new Vector<Entity>();

		PassbookService service = new PassbookService();
		List result = service.getPassbookFormList();
		Iterator e = result.iterator();
		while (e.hasNext()) {
			vect.add((Entity) e.next());
		}
		return vect;
	}

	@Override
	public String getPageName() {
		// TODO Auto-generated method stub
		return "PassbookPage";
	}

	@Override
	public void update(IPassbookEvent event) {
		// TODO Auto-generated method stub
		if (event.getEventType().isAlso(
				IPassbookEventType.PassbookTableChangeEvent)) {
			tableMode.setData(getData());

		}
	}

	@Override
	public void disposePage() {
		// TODO Auto-generated method stub
		super.disposePage(); // removeIPassbookListener

		thisPage = null;
	}
}
